package com.local.videoplayer.activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
import com.local.videoplayer.R;
import com.local.videoplayer.patternview.PatternView;
import com.local.videoplayer.utils.LogUtil;
import com.local.videoplayer.utils.MySpUtils;
import com.local.videoplayer.utils.ToastUtil;

/**
 * Created by ccx on 17/8/15.
 */

public class LockViewActivity extends BaseActivity {

    private PatternView patternView;

    private String patternString;
    private TextView mNoticeTv;
    private String mLockFileCode;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_lockview);
        findViewByIdExt(R.id.ll_back).setOnClickListener(this);
        patternView = (PatternView) findViewById(R.id.patternView);
        patternView.setTactileFeedbackEnabled(false);
        mNoticeTv = findViewByIdExt(R.id.tv_notice);
        mLockFileCode = MySpUtils.getLockFileCode();
        if (TextUtils.isEmpty(mLockFileCode)) {
            mNoticeTv.setText("Draw a pattern");
        } else {
            mNoticeTv.setText("Draw your pattern");
        }
        Toast.makeText(getApplicationContext(), "ENTER PATTERN", Toast.LENGTH_LONG).show();
        patternView.setPathColor(getResources().getColor(R.color.title_bg));
        patternView.setDotColor(getResources().getColor(R.color.title_bg));
        patternView.setCircleColor(getResources().getColor(R.color.title_bg));
        patternView.setOnPatternDetectedListener(new PatternView.OnPatternDetectedListener() {

            @Override
            public void onPatternDetected() {
                if (patternView.getPatternString().split("&").length <=3){
                    ToastUtil.toast("Pattern is too short");
                    patternView.clearPattern();
                    return;
                }
                if (mLockFileCode == null) {
                    //未设置密码
                    if (patternString == null) {
                        //设置第一次
                        patternString = patternView.getPatternString();
                        patternView.clearPattern();
                        mNoticeTv.setText("Confirm your pattern");
                    } else {
                        //设置确认
                        if (patternString.equals(patternView.getPatternString())) {
                            MySpUtils.setLockFileCode(patternString);
                            //// TODO: 17/8/15 跳转私密文件夹
                        } else {

                            ToastUtil.toast("Wrong,try Again");
                            mNoticeTv.setText("Wrong,try Again");
                            mNoticeTv.setText("Draw a pattern");
                            patternString = null;
                            patternView.clearPattern();
                        }
                    }
                } else {
                    if (mLockFileCode.equals(patternView.getPatternString())) {
                        //// TODO: 17/8/15 跳转私密文件夹
                    } else {
                        ToastUtil.toast("Wrong,try Again");
                        mNoticeTv.setText("Wrong,try Again");
                        mNoticeTv.setText("Draw your pattern");
                        patternString = null;
                        patternView.clearPattern();
                    }
                }

            }
        });
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {
            case R.id.ll_back:
                finish();
                break;
        }
    }
}
